package com.example.qiandao.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
@Data
@Entity
@Table(name = "class")
@EntityListeners(AuditingEntityListener.class)
public class Clazz {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String className;
    private String classCode;

    @ManyToMany(mappedBy = "classes")
    private Set<User> students;

    @ManyToOne
    @JoinColumn(name = "course_id")
    @JsonBackReference
    private Course course;

    @OneToMany(mappedBy = "clazz", cascade = CascadeType.ALL)
    private Set<CheckinRecord> checkinRecords;
    @OneToMany(mappedBy = "clazz", cascade = CascadeType.ALL)
    private List<CheckinHistory> checkinHistories;

    @CreatedDate
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String name) {
        this.className = name;
    }

    public String getClassCode() {
        return classCode;
    }

    public void setClassCode(String code) {
        this.classCode = code;
    }

    public Set<User> getStudents() {
        return students;
    }

    public void setStudents(Set<User> students) {
        this.students = students;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public Set<CheckinRecord> getCheckins() {
        return checkinRecords;
    }

    public void setCheckins(Set<CheckinRecord> checkinRecords) {
        this.checkinRecords = checkinRecords;
    }

    public List<CheckinHistory> getCheckinHistories() {
        return checkinHistories;
    }

    public void setCheckinHistories(List<CheckinHistory> checkinHistories) {
        this.checkinHistories = checkinHistories;
    }

    // 新增时间字段的getter/setter
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    // 添加 toString 方法，便于调试
    @Override
    public String toString() {
        return "Clazz{" +
                "id=" + id +
                ", name='" + className + '\'' +
                '}';
    }
}
